#include<bits/stdc++.h>
#define int long long
using namespace std;
char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return f?-x:x;
}
int n,ans;
int a[410],b[410];
int q[410];
int vis[410],v[410];
int head[1010],tot;
struct Tree{
	int to,nxt;
}tree[1010];
void add(int x,int y){
	tree[++tot].to = y;
	tree[tot].nxt = head[x];
	head[x] = tot;
}
void dfs(int x,int num){
	q[num] = x;
	vis[x] = 1;
	if(num == n){
		for(int i = 1 ; i <= n ; i++) b[i] = a[i];
		memset(v,0,sizeof v);
		for(int i = 1 ; i <= n ; i++){
			v[q[i]] = 1;
			for(int j = head[q[i]] ; j ; j = tree[j].nxt){
				int to = tree[j].to;
				if(!v[to]) b[to] += b[q[i]];
			}
		}
		int s = 0;
		for(int i = 1 ; i <= n ; i++) s += b[i];
		ans = max(ans,s);
		vis[x] = 0;
		return ;
	}
//	for(int i = 1 ; i <= n ; i++)
//		cout << vis[i] <<" ";
//	cout << endl;
	for(int i = 1 ; i <= n ; i++)
	    if(!vis[i]) dfs(i,num + 1);
	   
	vis[x] = 0;
}
signed main(){
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
    n = read();
    for(int i = 1 ; i <= n ; i++) a[i] = read();
    for(int i = 1 ; i < n ; i++){
    	int x = read(),y = read();
    	add(x,y),add(y,x);
	}
	for(int i = 1 ; i <= n ; i++){
	    memset(vis,0,sizeof vis);
		dfs(i,1);
	}
	cout << ans << endl;
	return 0;
}

